import { Component, OnInit, ViewChild } from '@angular/core';
import { Select } from 'app/classes/Device';
import { FormsModule } from '@angular/forms'
import { TimePicker, TimePickerComponent } from 'app/time-picker/time-picker.component';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Rx';
declare var BootstrapDialog;
@Component({
  selector: 'app-rate',
  templateUrl: './rate.component.html',
  styleUrls: ['./rate.component.css']
})
export class RateComponent implements OnInit {
  @ViewChild(TimePickerComponent)
  public timePicker: TimePickerComponent;

  selects: Array<Select>;
  params = new Params();

  ths = ['名称', '开始时间' , '结束时间', '费率', '设置'];
  trs: Item[];
  url = '/DSMService/GetSPFV';
  //url = '/api/getV.php';
  public observable: Observable<any>;
  constructor(public http: Http) { }
  ngOnInit() {
    this.params.item.datatype = '1050';
    this.params.item.charge = 0;
    // 初始化数据类型
    this.selects = [
      new Select('尖', '1050'),
      new Select('峰', '1051'),
      new Select('平', '1052'),
      new Select('谷', '1053')
    ];
    this.observable = this.http.get(this.url).map( res => res.json() );
    this.observable.subscribe(
      data => {
        console.log(JSON.parse(data));
        this.trs = JSON.parse(data).data;
      },
      err => console.log(err)
    );
  }
  getTime(e: TimePicker) {
    switch (e.timeType) {
      case 'start': {
        this.params.item.begintime = e.timeValue
      }; break;
      default: {
        this.params.item.endtime = e.timeValue;
      }
    }
    console.log(this.params);
  }
  add() {
    BootstrapDialog.confirm({
      title: '通知',
      message: '确认要添加这条吗？',
      type: BootstrapDialog.TYPE_WARNING,
      btnOKClass: '确认',
      btnCancelLabel: '取消',
      callback: (result) => {
          if (result) {
            this.params.flag = 0;
            this.observable = this.http.post('/DSMService/SaveSPFV', this.params ).map( res => res.json() );
            this.observable.subscribe(
              data => {
                const _data = JSON.parse(data);
                console.log(JSON.parse(data))
                if (_data.status === '1') {
                  this.params.item.id = + _data.msg;
                  console.log(this.params.item)
                  this.trs.unshift( { ...this.params.item } );
                  BootstrapDialog.show({
                    type: BootstrapDialog.TYPE_SUCCESS,
                    title: '通知',
                    message: '添加成功'
                  });
                }
              },
              err => console.log(err)
            );
          }else {
            console.log('取消添加');
          }
      }
    });
  }
  del(id: number) {
    BootstrapDialog.confirm({
      title: '通知',
      message: '确认要添加这条吗？',
      type: BootstrapDialog.TYPE_WARNING,
      btnOKClass: '确认',
      btnCancelLabel: '取消',
      callback: (result) => {
          if (result) {
            this.params.flag = 2;
            this.params.item.id = id;
            this.observable = this.http.post('/DSMService/SaveSPFV', this.params ).map( res => res.json() );
            this.observable.subscribe(
              data => {
                const _data = JSON.parse(data);
                if (_data.status === '1') {
                  BootstrapDialog.show({
                    type: BootstrapDialog.TYPE_SUCCESS,
                    title: '通知',
                    message: '删除成功'
                  });
                  this.trs = this.trs.filter(item => item.id !== id)
                }
              },
              err => console.log(err)
            );
          }else {
            console.log('取消删除');
          }
      }
    });
  }
  update(id: number) {
    BootstrapDialog.show({
      message: $('<div></div>').load('assets/test.html')
    });
  }
  clear() {
    this.timePicker.clear();
  }
}

export class Params {
  item: Item = new Item;
  flag: number;
}
export class Item {
  begintime: string;
  endtime: string;
  datatype: string;
  charge: number;
  id: number;
}

